﻿using Blog190311.Models;
using Blog190311.Models.DBModel;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace Blog190311.Areas.Back.Models.DAL
{
    public class HomeService
    {
        public User GetUser(string username)
        {
            User user = null;
            string sql = string.Format("select * from [dbo].[User] where UserName ='{0}'", username);
            SqlDataReader dr = DBHelper.ExecuteReader(sql);
            if (dr != null)
            {
                if (dr.Read())
                {
                    user = new User
                    {
                        UserName = dr["UserName"].ToString(),
                        Password = dr["PassWord"].ToString(),
                        SelfSlogo = dr["SelfSlogo"].ToString(),
                        SelfPhoto = dr["SelfPhoto"].ToString(),
                        LastLoginTime = dr["LastLoginTime"].ToString(),
                        AboutMe = dr["AboutMe"].ToString()
                    };
                }
            }
            return user;
        }

        public int UpdateUser(User user)
        {
            string sql = string.Format("update [User] set Password ='{0}',LastLoginTime ='{1}' where UserName ='{2}'", user.Password, user.LastLoginTime, user.UserName);
            int re = DBHelper.ExecuteNonQuery(sql);
            return re;
        }

        public User GetUserByUserName(string username)
        {
            string sqlStr = string.Format("select * from [User] where username='{0}'", username);
            SqlDataReader dr = DBHelper.ExecuteReader(sqlStr);
            if (dr.Read())
            {
                User user = new User()
                {
                    UserName = dr["UserName"].ToString(),
                    Password = dr["Password"].ToString()
                };
                return user;
            }
            return null;
        }

        public List<User> GetUser()
        {
            List<User> list = new List<User>();
            string sql = "select * from [dbo].[User] ";
            SqlDataReader dr = DBHelper.ExecuteReader(sql);
           if (dr.Read())
           {
                list.Add(new User()
                {
                    UserName = dr["UserName"].ToString(),
                    Password = dr["PassWord"].ToString(),
                    SelfSlogo = dr["SelfSlogo"].ToString(),
                    SelfPhoto = dr["SelfPhoto"].ToString(),
                    LastLoginTime = dr["LastLoginTime"].ToString(),
                    AboutMe = dr["AboutMe"].ToString()
                });
           }
            return list;
        }
    }
}